<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use app\admin\service\MinerFirmwareversion as MinerFirmwareversionService;
use app\admin\controller\Base;
use think\facade\Db;

class MinerFirmwareversion extends Base
{

    protected $request;
    protected $service;
    protected $validate;
    protected $operator_id;

    public function __construct(Request $request, MinerFirmwareversionService $service)
    {
        $this->request = $request;
        $this->service = $service;
        $this->validate = new \app\admin\validate\MinerFirmwareversion();
        $this->operator_id = $request->operator_id;
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = empty($this->request->params) ? [] : $this->request->params;

        $pageIndex         = isset($params['pageIndex'])             ? intval($params['pageIndex'])             : 1;
        $pageSize          = isset($params['pageSize'])              ? intval($params['pageSize'])              : 1000;
        $productname       = isset($params['productname'])           ? trim($params['productname'])             : '';
        $version_id        = isset($params['version_id'])            ? trim($params['version_id'])              : '';
        $release_date      = isset($params['release_date'])          ? trim($params['release_date'])            : '';

        if (!empty($productname)) {
            $pids = Db::name('product')->where('cname', 'like', '%' . $productname . '%')->where('is_del', 0)->column('id');
            $where[] = ['pid', 'in', $pids];
        }
        if (!empty($version_id)) {
            $where[] = ['version_id', 'like', '%' . $version_id . '%'];
        }
        if (!empty($release_date)) {
            $release_date_arr = explode(',', $release_date);
            $where['release_date'] = ['between', [$release_date_arr[0], $release_date_arr[1]]];
        }

        $where[] = ['is_del' => 0];

        $field = '*';
        $res =  $this->service->getList($pageIndex, $pageSize, $where, $field);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('create')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }
        $params['creator'] = $this->operator_id;
        $params['create_time'] = date('Y-m-d H:i:s');
        $res = $this->service->create($params);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";

        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $request)
    {
        $params = $this->request->params;
        $res = $this->service->getInfo($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }

        $params['updater'] = $this->operator_id;
        $params['update_time'] = date('Y-m-d H:i:s');
        $res = $this->service->update($params);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = $this->service->delete($id);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
